// pages2/pages/index/addressBook/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import * as Storage from '../../../../utils/storage'
const $api = require("../../../../request/api").API
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		equipId: "",
		equipType: 1, //呼叫器 2 其他类型1
		list: [{
			linkName: "",
			linkPhone: ""
		}, ]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad({
		equipId,
		equipType
	}) {
		this.setData({
			equipId,
			equipType
		})
		this.getData()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},
	async getData() {
		let res = await $api.getSafeEquipContact({
			equipId: this.data.equipId
		})
		res = res.data.data
		if (!res || !res.linkNames) return
		let {
			linkNames,
			linkPhones
		} = res
		if (linkNames) {
			let list = []
			linkNames = linkNames.split(",")
			linkPhones = linkPhones.split(",")
			linkNames.forEach((item, index) => list.push({
				linkName: item,
				linkPhone: linkPhones[index]
			}))
			this.setData({
				list
			})
		}
	},
	onAdd() {
		const {
			list
		} = this.data
		if (this.data.equipType == 2) {
			if (list.length == 4) return Toast.fail("最多只可以添加四个")
		} else {
			if (list.length == 5) return Toast.fail("最多只可以添加五个")
		}

		list.push({
			linkName: "",
			linkPhone: ""
		})
		this.setData({
			list
		})
	},
	onInput({
		currentTarget,
		detail
	}) {
		const {
			prop,
			index
		} = currentTarget.dataset
		let key = `list[${index}].${prop}`
		this.setData({
			[key]: detail.value,
		})
	},
	onDelete(e) {
		const {
			index,
			prop
		} = e.currentTarget.dataset
		const {
			list
		} = this.data
		if (prop) {
			list[index][prop] = ""
		} else {
			list.splice(index, 1)
		}
		this.setData({
			list
		})
	},
	checkData() {
		const {
			list
		} = this.data
		const phoneReg = /^\d+$/
		if (!list.length) {
			Toast.fail("至少填写一个联系人和联系电话")
			return
		}
		for (let index in list) {
			if (!list[index].linkName) {
				Toast.fail("请输入联系人")
				return
			}
			if (!list[index].linkPhone) {
				Toast.fail("请输入联系电话")
				return
			}

			if (list[index].linkPhone && !phoneReg.test(list[index].linkPhone)) {
				Toast.fail(`第${Number(index)+1}个电话号码格式错误`)
				return
			}

		}
		return true
	},
	submit() {
		if (!this.checkData()) return
		Dialog.confirm({
			title: "提示",
			message: "确定保存"
		}).then(() => {
		  Toast.loading({
        message: '请等待...',
        duration: 0
      });
			const {
				list,
				equipId
			} = this.data
			let linkNames = []
			let linkPhones = []
			list.forEach(item => linkNames.push(item.linkName) && linkPhones.push(item.linkPhone))
			const params = {
				equipId,
				linkNames: linkNames.toString(),
				linkPhones: linkPhones.toString()
			}
			$api.setSafeEquipContact(params).then(res => {
				if(res.data.status==0){
					Toast.success("设置成功")
				}else{
					Toast.fail(res.data.msg)
				}
			}).catch(err=>{
				Toast.fail(err.msg)
			})
		})
		.catch(()=>{

		})

	},

})